package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.EventsVo1;
import com.hlingsoft.kkh.entity.vo.StartsVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/6/24.
 */
@Controller
@RequestMapping("v20/collect")
public class CollectAction extends BaseAction {

    protected final int rows=10;

    /**
     * 添加收藏
     * @param userId
     * @param eventsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> like(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "eventsId")  Integer eventsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //判断之前是否有记录
            Collect checkCollect = collectDao.getByIdsWithNone(userId, eventsId);
            if (checkCollect!=null){
                checkCollect.setAvailable(1);
                collectDao.updateCollect(checkCollect);
                resultMap.put("collect",checkCollect);
            }  else {
                Date now = new Date();
                Collect collect = new Collect();
                collect.setAppUserId(userId);
                collect.setEventsId(eventsId);
                collect.setCreateTime(now);
                collect.setUpdateTime(now);

                collectDao.addCollect(collect);
                resultMap.put("collect",collect);

            }

            List<AppUser> collectUserList = new ArrayList<AppUser>();

            EventsVo1 eventsVo1 = new EventsVo1();
            //活动id
            eventsVo1.setId(eventsId);

            Events events = eventsDao.getEventsById(eventsId);
            //活动名
            eventsVo1.setName(events.getName());
            //活动图片
            EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
            List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
            eventsVo1.setImageList(imageList);
            eventsVo1.setEventCommentsId(firstEventComments.getId());
            //活动发布人信息
            eventsVo1.setAppUserId(events.getAppUserId());
            eventsVo1.setAppUser(userDao.getUserById(events.getAppUserId()));
            //活动描述
            eventsVo1.setDescription(events.getDescription());

            //评论
            List<Reply> replyList = replyDao.getReplyByEventCommentsId(firstEventComments.getId(), 0, 2);
            Collections.sort(replyList, new Comparator<Reply>() {
                @Override
                public int compare(Reply o1, Reply o2) {
                    return o1.getCreateTime().compareTo(o2.getCreateTime());
                }
            });
            eventsVo1.setReplyList(replyList);
            //经纬度
            eventsVo1.setLatitude(events.getLatitude());
            eventsVo1.setLongitude(events.getLongitude());

            //创建时间
            eventsVo1.setCreateAddress(events.getCreateAddress());
            eventsVo1.setCreateTime(events.getCreateTime());
            eventsVo1.setUpdateTime(events.getUpdateTime());



            //收藏人
            List<Collect> collectList =  collectDao.getByEventsId(events.getId(),0,10);
            for (Collect collect:collectList){
                AppUser appUser = userDao.getUserById(collect.getAppUserId());
                if (appUser!=null){
                    collectUserList.add(appUser);
                }
            }
            eventsVo1.setCollectUserList(collectUserList);
            eventsVo1.setCollectCount(collectDao.getByEventsIdCount(events.getId()));

            //是否能收藏
            if (userId != 0) {
                if (collectDao.getByIds(userId,events.getId())==null) {
                    eventsVo1.setIsCollect(0);
                } else {
                    eventsVo1.setIsCollect(1);
                }
            } else {
                eventsVo1.setIsCollect(1);
            }

            resultMap.put("eventsVo1",eventsVo1);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 取消收藏
     * @param userId
     * @param eventsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/cancel.json")
    @ResponseBody
    public Map<String, Object> cancel(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "eventsId")  Integer eventsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Collect checkCollect = collectDao.getByIds(userId, eventsId);
            if (checkCollect!=null){
              checkCollect.setAvailable(0);
                collectDao.updateCollect(checkCollect);

                List<AppUser> collectUserList = new ArrayList<AppUser>();
                EventsVo1 eventsVo1 = new EventsVo1();
                //活动id
                eventsVo1.setId(eventsId);

                Events events = eventsDao.getEventsById(eventsId);
                //活动名
                eventsVo1.setName(events.getName());
                //活动图片
                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
                eventsVo1.setImageList(imageList);
                eventsVo1.setEventCommentsId(firstEventComments.getId());
                //活动发布人信息
                eventsVo1.setAppUserId(events.getAppUserId());
                eventsVo1.setAppUser(userDao.getUserById(events.getAppUserId()));
                //活动描述
                eventsVo1.setDescription(events.getDescription());

                //评论

                List<Reply> replyList = replyDao.getReplyByEventCommentsId(firstEventComments.getId(), 0, 2);
                Collections.sort(replyList, new Comparator<Reply>() {
                    @Override
                    public int compare(Reply o1, Reply o2) {
                        return o1.getCreateTime().compareTo(o2.getCreateTime());
                    }
                });
                eventsVo1.setReplyList(replyList);

                //经纬度
                eventsVo1.setLatitude(events.getLatitude());
                eventsVo1.setLongitude(events.getLongitude());

                //创建时间
                eventsVo1.setCreateAddress(events.getCreateAddress());
                eventsVo1.setCreateTime(events.getCreateTime());
                eventsVo1.setUpdateTime(events.getUpdateTime());



                //收藏人
                List<Collect> collectList =  collectDao.getByEventsId(events.getId(),0,10);
                for (Collect collect:collectList){
                    AppUser appUser = userDao.getUserById(collect.getAppUserId());
                    if (appUser!=null){
                        collectUserList.add(appUser);
                    }
                }
                eventsVo1.setCollectUserList(collectUserList);
                eventsVo1.setCollectCount(collectDao.getByEventsIdCount(events.getId()));

                //是否能收藏
                if (userId != 0) {
                    if (collectDao.getByIds(userId,events.getId())==null) {
                        eventsVo1.setIsCollect(0);
                    } else {
                        eventsVo1.setIsCollect(1);
                    }
                } else {
                    eventsVo1.setIsCollect(1);
                }

                resultMap.put("eventsVo1",eventsVo1);
            } else {
                resultMap.put("msg","您并未关注此社区");
            }


            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 收藏人列表
     * @param userId
     * @param eventsId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> edit(
            @RequestParam(value = "userId",required = false) Integer userId,
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<StartsVo> startsVoList = new ArrayList<StartsVo>();
            List<Collect> collectList = collectDao.getByEventsId(eventsId, (pageNum - 1) * rows, rows);
            for (Collect collect:collectList){
                StartsVo startsVo = new StartsVo();
                //获取关注人
                AppUser appUser = userDao.getUserById(collect.getAppUserId());
                //获取关注人粉丝数
                long fansCount = focusDao.getFansByIdCount(appUser.getId());
                appUser.setFansCount(fansCount);
                //判断是否可以关注
                if (userId!=null){
                    if (focusDao.getFocus(userId,appUser.getId()) == null) {
                        appUser.setIsFocus(0);
                    } else {
                        appUser.setIsFocus(1);
                        if (focusDao.getFocus(appUser.getId(),userId)!=null){
                            appUser.setIsFocus(2);
                        }
                    }
                }else {
                    appUser.setIsFocus(0);
                }

                startsVo.setAppUser(appUser);
                Events events= eventsDao.getNewestEventsByUserId(appUser.getId());
                startsVo.setNewestEvents(events);

                startsVoList.add(startsVo);
            }
            resultMap.put("startsVoList",startsVoList);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
